package fun.anin.aninhongbao.receive;

import com.rabbitmq.client.Channel;
import fun.anin.aninhongbao.dto.WalletDTO;
import fun.anin.aninhongbao.entity.RedPacketInfo;
import fun.anin.aninhongbao.entity.RedPacketRecord;
import fun.anin.aninhongbao.service.RedPacketInfoService;
import fun.anin.aninhongbao.service.RedPacketRecordService;
import fun.anin.aninhongbao.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/***
 * 红包队列的接收方
 * @author anin
 * @date: 2020/3/31 18:57
 * @version : V1.0
 */
@Slf4j
@Component
public class RedPacketReceive {

    @Autowired
    private RedPacketRecordService recordService;
    @Autowired
    private RedPacketInfoService infoService;
    @Autowired
    private WalletService walletService;

    @RabbitHandler
    @RabbitListener(queues = "redpacket.back")
    public void back(Message message, Channel channel, RedPacketInfo redPacketInfo) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        infoService.backRedPacket(redPacketInfo);
    }

    @RabbitHandler
    @RabbitListener(queues = "redpacket.record")
    public void record(Message message, Channel channel,RedPacketRecord record) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        recordService.save(record);
    }

    @RabbitHandler
    @RabbitListener(queues = "redpacket.markAsLuck")
    public void markAsLuck(Message message, Channel channel,RedPacketRecord record) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        recordService.markAsLuck(record);
    }
}
